#!/bin/sh

latestChunk="latest-chunk.txt"
chunk="chunk.txt"
echo $ROLLBACK_VERSION_ID $env
path=$1
project=$2
diff="diff-${project}-${env}.txt"

touch $chunk
find $path -type f -name '*.js' -o -name '*css' | sed 's@.*/@@' > $chunk

# cat $chunk

rsync -e "ssh -o StrictHostKeyChecking=no -p 65022" -av \
  deploy_html@119.91.221.218:"/usr/share/nginx/html/ci-build/$project/$env/$latestChunk" \
  $latestChunk

if [ ! -f "$latestChunk" ]; then
  touch $latestChunk
fi

# cat $latestChunk

touch $diff
echo "-----------new-------------" >> $diff
sort $chunk $latestChunk $latestChunk | uniq -u >> $diff
echo "---------deleted-----------" >> $diff
sort $latestChunk $chunk $chunk | uniq -u >> $diff
echo "--------unchanged----------" >> $diff
sort $chunk $latestChunk | uniq -d >> $diff

# cat $diff

rsync -e "ssh -o StrictHostKeyChecking=no -p 65022" -av \
  $chunk \
  deploy_html@119.91.221.218:"/usr/share/nginx/html/ci-build/$project/$env/$latestChunk"

rsync -e "ssh -o StrictHostKeyChecking=no" -av \
  $diff \
  ubuntu@212.129.164.220:/usr/share/nginx/html/sourcemap/

